package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @BelongsProject: itcast_health
 * @BelongsPackage: com.itheima.health.service.impl
 * @Author: ${Shiu}
 * @CreateTime: 2020-02-22 23:03
 * @Description: 菜单业务层接口实现类
 */
@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

    @Override
    public void delete(Integer id) {
        Long count = menuDao.countCheckItemsById(id);
        if (count >0){
            //说明检查组中使用了该检查项, 该检查项不能被删除
            throw new RuntimeException("检查组中使用了该检查项, 该检查项不能被删除");
        }
        //说明 当前的检查项没有被引用, 可以删除
        menuDao.deleteById(id);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //调用dao, 获取检查项数据
        Page<Menu> page = menuDao.selectByCondition(queryPageBean.getQueryString());
        //创建PageResult结果对象( 检查项的总记录数, 当前页显示需要的10条检查项数据 )
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    /**
     * 查询所有菜单
     * @return
     */
    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }
}
